Remove debug prints and turn logging to stderr off by default.
"""
self.reap_cancel()
domlist = xc.domain_getinfo()
- #for d in domlist:
- # print 'reap> xen: ', d['dom'], d
- #for d in self.domain_by_id.values():
- # print 'reap> xend: ', d.id, d.info
casualties = []
for d in domlist:
dead = 0
"""
self.refresh_cancel()
domlist = xc.domain_getinfo()
- #for d in domlist:
- # print 'refresh> xen: ', d['dom'], d
- #for d in self.domain_by_id.values():
- # print 'refresh> xend: ', d.id, d.info
# Index the domlist by id.
# Add entries for any domains we don't know about.
doms = {}
@param vmconfig: vm configuration
@return: deferred
"""
- print 'domain_configure>', id, vmconfig
config = sxp.child_value(vmconfig, 'config')
dominfo = self.domain_lookup(id)
- print 'domain_configure>', 'dominfo=', dominfo
- for dinfo in self.domain_by_id.values():
- print 'domain', 'id=', dinfo.id, 'name=', dinfo.name
log.debug('domain_configure> id=%s config=%s', str(id), str(config))
if dominfo.config:
raise XendError("Domain already configured: " + dominfo.id)
"""
# Need a cancel too?
# Don't forget to cancel restart for it.
- print 'domain_migrate>', id, dst
dominfo = self.domain_lookup(id)
xmigrate = XendMigrate.instance()
val = xmigrate.migrate_begin(dominfo.id, dst, live=live)
- print 'domain_migrate<', val
return val
def domain_save(self, id, dst, progress=0):
xmigrate = XendMigrate.instance()
return xmigrate.save_begin(dominfo.id, dst)
- def domain_pincpu(self, dom, cpu):
+ def domain_pincpu(self, id, cpu):
"""Pin a domain to a cpu.
- @param dom: domain
+ @param id: domain
@param cpu: cpu number
"""
dominfo = self.domain_lookup(id)
import time
from twisted.internet import defer
-#defer.Deferred.debug = 1
import xen.lowlevel.xc; xc = xen.lowlevel.xc.new()
import xen.util.ip
def arping(ip, gw):
cmd = '/usr/sbin/arping -A -b -I eth0 -c 1 -s %s %s' % (ip, gw)
- print cmd
+ log.debug(cmd)
os.system(cmd)
gateway = xen.util.ip.get_current_ipgw() or '255.255.255.255'
# my domain id.
if not dominfo:
return
- #print 'check_name>', 'dom=', dominfo.name, dominfo.dom, 'self=', name, self.dom
if dominfo.is_terminated():
return
if not self.dom or (dominfo.dom != self.dom):
dateFormat = "%Y-%m-%d %H:%M:%S"
def __init__(self, filename, level=logging.INFO, maxBytes=None, backupCount=None):
+ """Initialise logging. Logs to 'filename' by default, but does not log to
+ stderr unless addLogStderr() is called.
+ """
self.setLevel(level)
if maxBytes:
self.maxBytes = maxBytes
self.backupCount = backupCount
self.initLogFile(filename)
self.initLogStderr()
- pass
def setLevel(self, level):
if isinstance(level, types.StringType):
return logging.getLogger("xend")
def initLogFile(self, filename):
+ """Create the file logger and add it.
+ """
self.logfile = RotatingFileHandler(filename,
mode='a',
maxBytes=self.maxBytes,
return self.logfilename
def initLogStderr(self):
+ """Create the stderr logger, but don't add it.
+ """
self.logstderr = StreamHandler()
self.logstderr.setFormatter(Formatter(self.logStderrFormat, self.dateFormat))
+
+ def addLogStderr(self):
+ """Add logging to stderr."""
self.getLogger().addHandler(self.logstderr)
+
+ def removeLogStderr(self):
+ """Remove logging to stderr."""
+ self.getLogger().removeHandler(self.logstderr)
def getLogStderr(self):
return self.logstderr
self.dbroot = None
self.config_path = None
self.config = None
+ self.logger = None
self.configure()
self.check_lastboot()
eserver.subscribe('xend.*', self.event_handler)
def start(self):
eserver.inject('xend.start', self.rebooted)
+ def _format(self, msg, args):
+ if args:
+ return str(msg)
+ else:
+ return str(msg) % args
+
+ def _log(self, mode, fmt, args):
+ """Logging function that uses the logger if it exists, otherwise
+ logs to stderr. We use this for XendRoot log messages because
+ they may be logged before the logger has been configured.
+ Other components can safely use the logger.
+ """
+ log = self.get_logger()
+ if mode not in ['warning', 'info', 'debug', 'error']:
+ mode = 'info'
+ level = mode.upper()
+ if log:
+ getattr(log, mode)(fmt, *args)
+ else:
+ print >>stderr, "xend", "[%s]" % level, self._format(msg, args)
+
+ def logDebug(self, fmt, *args):
+ """Log a debug message.
+
+ @param fmt: message format
+ @param args: arguments
+ """
+ self._log('info', fmt, args)
+
+ def logInfo(self, fmt, *args):
+ """Log an info message.
+
+ @param fmt: message format
+ @param args: arguments
+ """
+ self._log('info', fmt, args)
+
+ def logWarning(self, fmt, *args):
+ """Log a warning message.
+
+ @param fmt: message format
+ @param args: arguments
+ """
+ self._log('warning', fmt, args)
+
+ def logError(self, fmt, *args):
+ """Log an error message.
+
+ @param fmt: message format
+ @param args: arguments
+ """
+ self._log('error', fmt, args)
+
def event_handler(self, event, val):
- print >> sys.stderr, "EVENT>", event, val
+ self.logInfo("EVENT> %s %s", str(event), str(val))
def read_lastboot(self):
+ """Read the lastboot file to determine the time of the last boot.
+ """
try:
val = file(self.lastboot, 'rb').readlines()[0]
except StandardError, ex:
- print 'warning: Error reading', self.lastboot, ex
+ self.logWarning('Error reading %s: %s', self.lastboot, str(ex))
val = None
return val
def write_lastboot(self, val):
+ """Write the last boot time to the lastboot file.
+ """
if not val: return
try:
fdir = os.path.dirname(self.lastboot)
out.write(val)
out.close()
except IOError, ex:
- print 'warning: Error writing', self.lastboot, ex
- pass
+ self.logWarning('Error writing %s: %s', self.lastboot, str(ex))
def check_lastboot(self):
"""Check if there has been a system reboot since we saved lastboot.
self.last_reboot = this_val
def get_last_reboot(self):
+ """Get the last reboot time as a string.
+ """
return self.last_reboot
def get_rebooted(self):
+ """Get the rebooted flag. The flag is true if the system has
+ been rebooted since xend was last run.
+ """
return self.rebooted
def configure(self):
- print 'XendRoot>configure>'
self.set_config()
self.configure_logger()
self.dbroot = self.get_config_value("dbroot", self.dbroot_default)
self.logging = XendLogging(logfile, level=loglevel)
def get_logging(self):
+ """Get the XendLogging instance.
+ """
return self.logging
def get_logger(self):
+ """Get the logger.
+ """
return self.logging.getLogger()
def get_dbroot(self):
The config file is a sequence of sxp forms.
"""
self.config_path = os.getenv(self.config_var, self.config_default)
- print 'XendRoot>set_config> config_path=', self.config_path
if os.path.exists(self.config_path):
- print 'XendRoot>set_config> loading'
fin = file(self.config_path, 'rb')
try:
config = sxp.parse(fin)
finally:
fin.close()
else:
- print 'XendRoot>set_config> not found'
self.config = ['xend-config']
- print 'XendRoot> config=', self.config
def get_config(self, name=None):
"""Get the configuration element with the given name, or
return v in ['yes', '1', 'on']
def instance():
+ """Get an instance of XendRoot.
+ Use this instead of the constructor.
+ """
global inst
try:
inst
return inst
def logger():
+ """Get the logger.
+ """
return instance().get_logger()
def add_component(name, val):
+ """Register a component with XendRoot.
+ This is used to work-round import cycles.
+
+ @param name: component name
+ @param val: component value (often a module)
+ """
return instance().add_component(name, val)
def get_component(name):
+ """Get a component.
+ This is used to work-round import cycles.
+
+ @param name: component name
+ @return component or None
+ """
return instance().get_component(name)
self.events = events
def queue_event(self, name, v):
- # Despite the name we dont' queue the event here.
+ # Despite the name we don't queue the event here.
# We send it because the transport will queue it.
self.send_event([name, v])
eserver.inject(sxp.name(event), event)
return ['ok']
- def op_traceon(self, name, v):
- self.daemon.tracing(1)
+ def op_trace(self, name, v):
+ mode = (v[1] == 'on')
+ self.daemon.tracing(mode)
- def op_traceoff(self, name, v):
- self.daemon.tracing(0)
+ def op_log_stderr(self, name, v):
+ mode = v[1]
+ logging = XendRoot.instance().get_logging()
+ if mode == 'on':
+ logging.addLogStderr()
+ else:
+ logging.removeLogStderr()
+
+ def op_debug_msg(self, name, v):
+ mode = v[1]
+ import messages
+ messages.DEBUG = (mode == 'on')
+
+ def op_debug_controller(self, name, v):
+ mode = v[1]
+ import controller
+ controller.DEBUG = (mode == 'on')
class EventFactory(protocol.Factory):
def respond_be_create(self, msg):
val = unpackMsg('blkif_be_create_t', msg)
- print 'respond_be_create>', val
self.connected = 1
return self
@type msg: xu message
"""
val = unpackMsg('blkif_be_connect_t', msg)
- print 'respond_be_connect>', str(self), val
self.send_fe_interface_status_changed()
def send_fe_interface_status_changed(self, response=None):
def recv_fe_driver_status_changed(self, msg, req):
val = unpackMsg('blkif_fe_driver_status_changed_t', msg)
- print 'recv_fe_driver_status_changed>', val
# For each backend?
msg = packMsg('blkif_fe_interface_status_changed_t',
{ 'handle' : 0,
import channel
from messages import msgTypeName, printMsg
-DEBUG = 1
+DEBUG = 0
class Responder:
"""Handler for a response to a message with a specified id.
vnet.vifctl(op, self.get_vifname(), self.get_mac())
def attach(self):
- print 'attach>'
d = self.send_be_create()
d.addCallback(self.respond_be_create)
return d
def respond_be_create(self, msg):
val = unpackMsg('netif_be_create_t', msg)
- print 'respond_be_create>', val
return self
def destroy(self):